<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>取得上一步 INSERT 操作产生的 ID</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.mysql-insert-id" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_insert_id</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_insert_id</span> &mdash; <span class="dc-title">
     取得上一步 INSERT 操作产生的 ID
    </span></p>

   </div>
   <div class="refsect1 unknown-unknown-unknown-unknown-unknown-unknown-seealsu" id="refsect1-function.mysql-insert-id-unknown-unknown-unknown-unknown-unknown-unknown-seealsu">
    <h3 class="title">说明</h3>
    <div class="methodsynopsis dc-description">
     <span class="methodname"><strong>mysql_insert_id</strong></span>
      ([ <span class="methodparam"><span class="type">resource</span> <code class="parameter">$link_identifier</code></span>
    ] ) : <span class="type">int</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>mysql_insert_id()</strong></span>
     返回给定的
     <code class="parameter">link_identifier</code>
     中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号。如果没有指定
     <code class="parameter">link_identifier</code>，则使用上一个打开的连接。
    </p>
    <p class="para">
     如果上一查询没有产生
     AUTO_INCREMENT 的值，则
     <span class="function"><strong>mysql_insert_id()</strong></span>
     返回 0。如果需要保存该值以后使用，要确保在产生了值的查询之后立即调用
     <span class="function"><strong>mysql_insert_id()</strong></span>。
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      MySQL 中的 SQL 函数
      <em>LAST_INSERT_ID()</em>
      总是保存着最新产生的
      AUTO_INCREMENT 值，并且不会在查询语句之间被重置。
    </p>
    </p></blockquote>
    <div class="warning"><strong class="warning">Warning</strong>
     <p class="para">
      <span class="function"><strong>mysql_insert_id()</strong></span> 将
      MySQL 内部的 C API 函数
      <em>mysql_insert_id()</em>
      的返回值转换成
      <em>long</em>（PHP 中命名为 <span class="type"><a href="language.types.integer.html" class="type int">int</a></span>）。如果
      AUTO_INCREMENT 的列的类型是 BIGINT，则
      <span class="function"><strong>mysql_insert_id()</strong></span>
      返回的值将不正确。可以在 SQL 查询中用
      MySQL 内部的 SQL 函数
      <em>LAST_INSERT_ID()</em> 来替代。
     </p>
    </div>
    <p class="para">
     <div class="example" id="example-2155">
      <p><strong>Example #1 <span class="function"><strong>mysql_insert_id()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"mysql_user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"mysql_password"</span><span style="color: #007700">)&nbsp;or<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Could&nbsp;not&nbsp;connect:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"mydb"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;mytable&nbsp;(product)&nbsp;values&nbsp;('kossu')"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"Last&nbsp;inserted&nbsp;record&nbsp;has&nbsp;id&nbsp;%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mysql_insert_id</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     参见 <span class="function"><a href="mysql_query.html" class="function">mysql_query()</a></span>。
    </p>
   </div>

  </div></div></div></body></html>